from pandac.PandaModules import *    # basic Panda modules
from direct.actor.Actor import Actor # for animated models
from direct.interval.IntervalGlobal import *  # for compound intervals
from direct.task import Task         # for update fuctions
import math
from projectile import Projectile
import cfg

#A simple function to make sure a value is in a given range, -1 to 1 by default
def restrain(i, mn = -1, mx = 1):
    return min(max(i, mn), mx)

class Turret(object):
    def __init__(self, owner, type = "plasma", targetObject = 0):
        self.dx = 0
        self.dy = 0
        self.dz = 0
        self.owner = owner
        self.model = Actor("../models/turret")
        self.model.hide()
        #self.swivelBase = self.model.controlJoint(None, 'modelRoot', 'swivel')
        #self.arm = self.model.controlJoint(None, 'modelRoot', 'arm')
        taskMgr.add(self.aimTurret, "aimTurret")
        self.targetObject = targetObject
        self.type = type
        self.dx = 0
        self.dy = 0
        self.dz = 0

        #if self.type == "plasma":
            #taskMgr.doMethodLater(1.5, self.shoot, "turret firing")
        #if self.type == "acid":
            #taskMgr.doMethodLater(.4, self.shoot, "turret firing")
        #if self.type == "laser":
            #taskMgr.doMethodLater(1, self.shoot, "turret firing")

    def findTarget(self):
        #if False:
        #    #Check to make sure the mouse is readable
        #    if base.mouseWatcherNode.hasMouse():
        #        #get the mouse position as a Vec2. The values for each axis are from -1 to
        #        #1. The top-left is (-1,-1), the bottom right is (1,1)
        #        mpos = base.mouseWatcherNode.getMouse()
        #        self.arm.setP(-restrain(mpos.getY()) * 50)
        #        self.swivelBase.setH((restrain(mpos.getX()) * 100))

        if self.targetObject == 0:
            return cfg.player.hitNode.getPos(render)
        else:
            return self.targetObject.getPos(render)

    def dist(self, a, b):
        return math.sqrt((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])+(a[2]-b[2])*(a[2]-b[2]))

    def aimTurret(self, task):
        #self.pos = self.arm.getPos("render")
        self.pos = self.model.getPos(render)
        self.target = self.findTarget()
        self.dx = self.pos[0] - self.target[0]
        self.dy = self.pos[1] - self.target[1]
        self.dz = self.pos[2] - self.target[2]
        self.dt = math.sqrt(math.pow(self.dx, 2)+math.pow(self.dy, 2)+math.pow(self.dz, 2))
        self.dx /= self.dt
        self.dy /= self.dt
        self.dz /= self.dt

        #self.adjH = math.degrees(math.atan2(self.dy, self.dx)) + 40
        #self.adjP = math.degrees(math.atan2(self.dz, math.sqrt(math.pow(self.dx, 2) + math.pow(self.dy, 2)))) + 90
        #self.adjH -= self.model.getParent().getH()
        #self.adjP -= self.model.getParent().getP()
        #self.arm.setP(self.adjP)
        #self.swivelBase.setH(self.adjH)
        return Task.cont                        #Task continues infinitely

    def shoot(self, type):
        p = Projectile(self.owner, self.model.getPos(render), self.dx, self.dy, self.dz, self.targetObject, type)
        cfg.projectilelist.append(p.model)
        if type == "laser":
            snd = base.loader.loadSfx("../sounds/laser_shot.wav")
            snd.setVolume(.2)
            snd.play()
        if type == "acid":
            snd = base.loader.loadSfx("../sounds/acid_shot.wav")
            snd.setVolume(.2)
            snd.play()
        if type == "plasma":
            snd = base.loader.loadSfx("../sounds/plasma_shot.wav")
            snd.setVolume(.2)
            snd.play()
        #return Task.again
    